#pragma once


#include "../genesis/genesis_qt.hpp"

#include <QAbstractButton>
#include <QDir>
#include <QDockWidget>
#include <QGridLayout>
#include <QList>
#include <QPainter>
#include <QPaintEvent>
#include <QPixmap>
#include <QScrollArea>
#include <QString>
#include <QWidget>

#include <list>
using std::list;


const QString  TILE_STASH_DOCK_NAME   = "Tile Stash";
const QString  TILE_STASH_DEFAULT_DIR = "./tiles";

constexpr int TILE_STASH_TILE_ITEM_SCALING = 48;


class TileItem : public QAbstractButton
{
	Q_OBJECT

public: // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	TileItem(const QString & tilePath)
		: tile(tilePath)
	{
		
	}

	constexpr inline GenesisTileObject
	getTile(void) && noexcept { return tile; }

protected: // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	void
	paintEvent(QPaintEvent * event) override
	{
		QPainter p(this);

		p.drawPixmap(QRect( QPoint(0,0), size() ), tile);
	}
	
signals: // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	void sentToEditor(GenesisTileObject &);
	
private: // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	GenesisTileObject tile;
};


class TileStash : public QScrollArea
{
	Q_OBJECT

public: // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	explicit TileStash(QWidget * = nullptr);

	constexpr inline QDir
	get_tile_dir(void) noexcept { return tileDir; }
	
private: // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	
	static inline QDir tileDir = QDir(TILE_STASH_DEFAULT_DIR);
	
	QGridLayout       tileItemGrid = QGridLayout(this);
	list<TileItem *>  tileItems;
	
	void load_tiles_from_directory(const QDir & = tileDir);
												  
private slots: // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	void slot_receive_from_editor(void);
};
